<?php
class U extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->load->library('form_validation');
		//$this->load->library('FineUploader');
		// autoload.php
		// library: Thank_auth, database
		// helper: url
	}


	public function get($mode = null, $uid = null)
	{
		switch($mode)
		{
			case 'mini' :
				// get one user min information
				if(is_numeric($uid))
				{
					// because the field name is 'id' in the bdd
					$id = $uid;

					$query = $this->db->select('id, username, created, imgURL, mode, lat, lng')->get_where('users', array('id' => $id));
						
					/* create one master array of the records */
					$row = $query->row_array();

					// some improvements
					$row['username'] = ucfirst($row['username']);
					$row['created'] = $this->unix2human(time() - strtotime($row['created']));
						
					$results[0] = $row;
				}
				break;
			case 'users' :
					
				// get user item(s)
				//$query = $this->db->select('uid, id, title, description, price, lat, lng, username, added, modified, imgURL')->get_where('objectsusers', array('uid' => $this->tank_auth->get_user_id()));
				break;
			case 'user' :
					
				// get one user
				// because the field name is 'id' in the bdd
				$id = $uid;

				$query = $this->db->select('id, username, created, imgURL')->get_where('users', array('id' => $id));
					
				/* create one master array of the records */
				$row = $query->row_array();

				// some improvements
				$row['username'] = ucfirst($row['username']);
				$row['created'] = $this->unix2human(time() - strtotime($row['created']));
					
				$results[0] = $row;
				break;
		}
		header('Content-type: application/json');
		echo json_encode($results);
	}

	private function unix2human($unix) {

		//--------------------------------------------------
		// Maths

		$sec = $unix % 60;
		$unix -= $sec;

		$minSeconds = $unix % 3600;
		$unix -= $minSeconds;
		$min = ($minSeconds / 60);

		$hourSeconds = $unix % 86400;
		$unix -= $hourSeconds;
		$hour = ($hourSeconds / 3600);

		$daySeconds = $unix % 604800;
		$unix -= $daySeconds;
		$day = ($daySeconds / 86400);

		$week = ($unix / 604800);

		//--------------------------------------------------
		// Text

		$output = '';

		if ($week > 0)
		{
			$output = $week . ' week';
		}
		else if ($day > 0)
		{
			$output = $day . ' day';
		}
		else if ($output > $hour)
		{
			$output = $hour . ' hour';
		}
		else if ($output > $min)
		{
			$output = $min . ' min';
		}
		else if ($output > $sec)
		{
			$output = $sec . ' sec';
		}

		return $output;

	}
}
?>